package com.iaz.tech.tools.uml.file.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.iaz.tech.tools.uml.IUMLNeuronAble;
import com.iaz.tech.tools.uml.behavioral.usecase.impl.NeuronRoler;
import com.iaz.tech.tools.uml.behavioral.usecase.impl.NeuronUseCase;
import com.iaz.tech.tools.uml.relationship.impl.NeuronExtendRelationship;
import com.iaz.tech.tools.uml.relationship.impl.NeuronGeneralizationsRelationship;
import com.iaz.tech.tools.uml.relationship.impl.NeuronImportRelationship;
import com.iaz.tech.tools.uml.relationship.impl.NeuronIncludeRelationship;
import com.iaz.tech.tools.uml.relationship.impl.NeuronMergeRelationship;
import com.iaz.tech.tools.uml.relationship.impl.NeuronMessageRelationship;
import com.iaz.tech.tools.uml.relationship.impl.NeuronNestingConnectorsRelationship;
import com.iaz.tech.tools.uml.relationship.impl.NeuronNestingsRelationship;
import com.iaz.tech.tools.uml.relationship.impl.NeuronRealizationsRelationship;
import com.iaz.tech.tools.uml.relationship.impl.NeuronUseCaseRelationship;
import com.iaz.tech.tools.uml.structural.classstruct.impl.NeuronClassStruct;
import com.iaz.tech.tools.uml.structural.classstruct.impl.NeuronEnumStruct;
import com.iaz.tech.tools.uml.structural.classstruct.impl.NeuronFunctionStruct;
import com.iaz.tech.tools.uml.structural.classstruct.impl.NeuronInterfaceStruct;
import com.iaz.tech.tools.uml.structural.classstruct.impl.NeuronParameterStruct; 
import com.iaz.tech.tools.uml.structural.pack.impl.NeuronPackage;

/**
 * 根据UML 配置生成Use Case , Class Digram, 
 * 并根据工程需要，转换的对象
 */
public class UMLLoaderFactory {
    /**
     * 日志
     */
	private final static Logger logger = (Logger) LoggerFactory.getLogger(NeuronClassLoader.class);

    /**
     * 根据json Object 映射生成对象
     * @param jsonObj
     * @return
     */
    public static IUMLNeuronAble generateUML(JSONObject  jsonObj) {

        IUMLNeuronAble result = null;
        try {
            if (jsonObj == null){
                throw new Exception("入口参数jsonObj 输入错误，为空！");
            }
            if (!jsonObj.containsKey("uml")){
                throw new Exception("入口参数jsonObj 输入错误，未包含uml 字段！");
            }
            String _uml_string = jsonObj.getString("uml");
            if (_uml_string == null || _uml_string.isEmpty()){
                throw new Exception("入口参数jsonObj 输入错误，uml 字段为空！");
            }
            if (_uml_string.equals("ClassStruct")) {
                result = new NeuronClassStruct();
                NeuronClassLoader loader = new NeuronClassLoader();
                loader.parseJSON(result, jsonObj);
            } else if (_uml_string.equals("ParameterStruct")) {
                result = new NeuronParameterStruct();
                NeuronParameterLoader loader = new NeuronParameterLoader();
                loader.parseJSON(result, jsonObj);
            } else if (_uml_string.equals("FunctionStruct")) {
                result = new NeuronFunctionStruct();
                NeuronFunctionLoader loader = new NeuronFunctionLoader();
                loader.parseJSON(result, jsonObj);
            } else if (_uml_string.equals("ExtendRelationship")) {
                result = new NeuronExtendRelationship();
                NeuronExtendLoader loader = new NeuronExtendLoader();
                loader.parseJSON(result, jsonObj);
            } else if (_uml_string.equals("GeneralizationsRelationship")) {
                result = new NeuronGeneralizationsRelationship ();
                NeuronGeneralizationsLoader loader = new NeuronGeneralizationsLoader();
                loader.parseJSON(result, jsonObj);
            } else if (_uml_string.equals("ImportRelationship")) {
                result = new NeuronImportRelationship ();
                NeuronImportLoader loader = new NeuronImportLoader();
                loader.parseJSON(result, jsonObj);
            } else if (_uml_string.equals("NeuronEnumStruct")) {
                result = new NeuronEnumStruct();
                NeuronEnumLoader loader = new NeuronEnumLoader();
                loader.parseJSON(result, jsonObj);
            } else if (_uml_string.equals("IncludeRelationship")) {
                result = new NeuronIncludeRelationship ();
                NeuronIncludeLoader loader = new NeuronIncludeLoader();
                loader.parseJSON(result, jsonObj);
            } else if (_uml_string.equals("MergeRelationship")) {
                result = new NeuronMergeRelationship();
                NeuronMergeLoader loader = new NeuronMergeLoader();
                loader.parseJSON(result, jsonObj);
            } else if (_uml_string.equals("MessageRelationship")) {
                result = new NeuronMessageRelationship();
                NeuronMessageLoader loader = new NeuronMessageLoader();
                loader.parseJSON(result, jsonObj);
            } else if (_uml_string.equals("NestingConnectorsRelationship")) {
                result = new NeuronNestingConnectorsRelationship();
                NeuronNestingConnectorsLoader loader = new NeuronNestingConnectorsLoader();
                loader.parseJSON(result, jsonObj);
            } else if (_uml_string.equals("NestingsRelationship")) {
                result = new NeuronNestingsRelationship();
                NeuronNestingsLoader loader = new NeuronNestingsLoader();
                loader.parseJSON(result, jsonObj);
            } else if (_uml_string.equals("RealizationsRelationship")) {
                result = new NeuronRealizationsRelationship();
                NeuronRealizationsLoader loader = new NeuronRealizationsLoader();
                loader.parseJSON(result, jsonObj);
            } else if (_uml_string.equals("Roler")) {
                result = new NeuronRoler();
                NeuronRolerLoader loader = new NeuronRolerLoader();
                loader.parseJSON(result, jsonObj);
            } else if (_uml_string.equals("UseCase")) {
                result = new NeuronUseCase();
                NeuronUseCaseLoader loader = new NeuronUseCaseLoader();
                loader.parseJSON(result, jsonObj);
            } else if (_uml_string.equals("UseCaseRelationship")) {
                result = new NeuronUseCaseRelationship();
                NeuronUseCaseRelationLoader loader = new NeuronUseCaseRelationLoader();
                loader.parseJSON(result, jsonObj);
            } else if (_uml_string.equals("NeuronPackage")) {
                result = new NeuronPackage();
                NeuronPackageLoader loader = new NeuronPackageLoader();
                loader.parseJSON(result, jsonObj);
            } else if (_uml_string.equals("NeuronInterfaceStruct")) {
                result = new NeuronInterfaceStruct();
                NeuronInterfaceLoader loader = new NeuronInterfaceLoader();
                loader.parseJSON(result, jsonObj);
            }
        } catch (Exception e) {
            logger.error("工厂生成UML 对象失败：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    } 
}
